//
//  Duel.cpp
//  Test
//
//  Created by Pedram Ardeshirzadeh on 20-07-12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "Duel.h"

enum gamePick {rock, paper, scissor};

Duel::Duel(Player *p1, Player *p2) {
    player1 = p1;
    player2 = p2;
    player1->setOpponent(player2);
    player2->setOpponent(player1);
    gameMat = new GameMat(player1, player2);
}

void Duel::begin() {
    assignTurn(playerToBegin());
}

Player* Duel::playerToBegin() {
    int s1, s2;
    cout << "Choose between rock(0), paper(1) or scissor(2) (" << player1->getName() << ") ..." << endl;
    cin >> s1;
    cout << "Choose between rock(0), paper(1) or scissor(2) (" << player2->getName() << ") ..." << endl;
    cin >> s2;
    
    switch (s1) {
        case rock:
            if (s2 == scissor) return player1->firstToGo() ? player1 : player2;
            break;
        case paper:
            if (s2 == rock) return player1->firstToGo() ? player1 : player2;
            break;
        case scissor:
            if (s2 == paper) return player1->firstToGo() ? player1 : player2;
            break;
            
        default:
            break;
    }
    
    return player2->firstToGo() ? player2 : player1;
}

void Duel::assignTurn(Player *p) {
    p->setHasTurn(true);
    cout << p->getName() << " gets to go first.";
    p->getOpponent()->setHasTurn(false);
}

Player* Duel::getActivePlayer() {
    return player1->getHasTurn() ? player1 : player2;
}

void Duel::assignWinner(Player *p) {
    p->addWin();
}

void Duel::assignLoser(Player *p) {
    p->addLoss();
}

void Duel::end() {
}

string Duel::toString() {
    return "Duel between " + player1->toString() + " and " + player2->toString();
}

//Duel::~Duel() {
//    delete gameMat;
//}

ostream& operator <<(ostream &output, const Duel &d) {
    output << "Duel between " << *d.player1 << " and " << *d.player2;
    return output;
}
